package com.snow.common.service;

import com.snow.common.controller.SystemConfigController;
import com.snow.component.SystemConfigComponent;
import com.snow.exception.CommonRuntimeException;
import com.snow.jpa.BaseService;
import com.snow.model.ILoginUser;
import com.snow.common.entity.SystemConfigEntity;
import com.snow.common.entity.SystemConfigItemEntity;
import com.snow.common.repository.SystemConfigItemRepository;
import com.snow.common.repository.SystemConfigRepository;
import com.snow.service.IAuthComponent;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class SystemConfigService extends BaseService<SystemConfigRepository, SystemConfigEntity, Integer> {
    private final SystemConfigComponent systemConfigComponent;
    private final IAuthComponent authComponent;
    private final SystemConfigItemRepository systemConfigItemRepository;

    public SystemConfigService(SystemConfigRepository repository, SystemConfigComponent systemConfigComponent, IAuthComponent authComponent, SystemConfigItemRepository systemConfigItemRepository) {
        super(repository);
        this.systemConfigComponent = systemConfigComponent;
        this.authComponent = authComponent;
        this.systemConfigItemRepository = systemConfigItemRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    public void config(SystemConfigItemEntity configParams, String domain) {
        Optional<SystemConfigEntity> optionalConfig = repository.findById(configParams.getId());
        if (!optionalConfig.isPresent()) {
            throw new CommonRuntimeException("配置信息不存在，请重试");
        }
        SystemConfigEntity config = optionalConfig.get();
        ILoginUser currentUser = authComponent.getCurrentUser();
        if (configParams.getConfigObjId() == null) {
            if (config.getType() == 1) {
                configParams.setConfigObjId(String.valueOf(currentUser.getId()));
            } else if (config.getType() == 3) {
                configParams.setConfigObjId(String.valueOf(currentUser.getTenantId()));
            } else if (config.getType() == 4) {
                configParams.setConfigObjId(domain);
            } else if (config.getType() == 5) {
                configParams.setConfigObjId(String.valueOf(currentUser.getDeptId()));
            } else {
                throw new CommonRuntimeException("暂不支持的配置项");
            }
        }
        if (config.getConfigType() == 1) {
            List<SystemConfigItemEntity> configItemList = systemConfigComponent.getConfigs(config.getId(), configParams.getConfigObjId());
            SystemConfigItemEntity configItem = getSystemConfigItemEntity(configParams, configItemList, config);
            configItem.setConfigValue(configParams.getConfigValue());
            systemConfigItemRepository.save(configItem);
        } else {
            SystemConfigItemEntity configItem = new SystemConfigItemEntity();
            configItem.setCreateInfo(currentUser);
            configItem.setConfigId(config.getId());
            configItem.setConfigLabel(configParams.getConfigLabel());
            configItem.setConfigValue(configParams.getConfigValue());
            configItem.setConfigObjId(configParams.getConfigObjId());
            systemConfigItemRepository.save(configItem);
        }
    }

    private SystemConfigItemEntity getSystemConfigItemEntity(SystemConfigItemEntity configParams, List<SystemConfigItemEntity> configItemList, SystemConfigEntity config) {
        SystemConfigItemEntity configItem = null;
        if (configItemList.size() > 1) {
            throw new CommonRuntimeException("配置信息重复，请联系管理员处理");
        }
        if (configItemList.size() == 1) {
            configItem = configItemList.get(0);
        }
        if (configItem == null) {
            configItem = new SystemConfigItemEntity();
            ILoginUser currentUser = authComponent.getCurrentUser();
            configItem.setConfigId(config.getId());
            configItem.setCreateInfo(currentUser);
            configItem.setConfigLabel(config.getName());
            configItem.setConfigObjId(configParams.getConfigObjId());
        }
        return configItem;
    }

    @Transactional(rollbackFor = Exception.class)
    public List<SystemConfigEntity> searchConfigsDetails(SystemConfigController.SearchConfigDetailsParams params) {
        List<SystemConfigEntity> list = lambdaQuery().in("type", params.getTypes()).findAll();
        for (int i = 0; i < list.size(); i++) {
            SystemConfigEntity config = list.get(i);
            config.setConfig(systemConfigComponent.getConfig(config.getId()).orElse(null));
        }
        return list;
    }
}
